home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_vim.idb / usr / freeware / share / vim / doc / tips.txt.z / tips.txt
Encoding:
Text File  |  1998-10-28  |  17.1 KB  |  435 lines

  1. *tips.txt*      For Vim version 5.0.  Last modification: 1998 Feb 18
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Tips and ideas for using Vim                *tips*
  8.  
  9. Editing C programs                |C-editing|
  10. Finding where identifiers are used        |ident-search|
  11. Editing local HTML files (WWW)            |html-editing|
  12. Editing paragraphs without a line break        |edit-no-break|
  13. Switching screens in an xterm            |xterm-screens|
  14. Scrolling in Insert mode            |scroll-insert|
  15. Smooth scrolling                |scroll-smooth|
  16. Correcting common typing mistakes        |type-mistakes|
  17. Renaming files                    |rename-files|
  18. Speeding up external commands            |speed-up|
  19. Useful mappings                    |useful-mappings|
  20. Compressing the help files            |gzip-helpfile|
  21. Executing shell commands in a window        |shell-window|
  22. Using <> notation in autocommands        |autocmd-<>|
  23. Using the "User" event for a function        |User-function|
  24.  
  25. ==============================================================================
  26. Editing C programs                    *C-editing*
  27.  
  28. There are quite a few features in Vim to help you edit C program files.  Here
  29. is an overview with tags to jump to:
  30.  
  31. |C-indenting|        Automatically set the indent of line while typing
  32.             text.
  33. |=|            Re-indent a few lines.
  34. |format-comments|    Format comments.
  35.  
  36. |:checkpath|        Show all recursively included files.
  37. |[i|            Search for identifier under cursor in current and
  38.             included files.
  39. |[_CTRL-I|        Jump to match for "[i"
  40. |[I|            List all lines in current and included files where
  41.             identifier under the cursor matches.
  42. |[d|            Search for define under cursor in current and included
  43.             files.
  44.  
  45. |CTRL-]|        Jump to tag under cursor.
  46. |CTRL-T|        Jump back from tag.
  47.  
  48. |gd|            Go to Declaration of local variable under cursor.
  49. |gD|            Go to Declaration of global variable under cursor.
  50.  
  51. |gf|            Go to file name under the cursor.
  52.  
  53. |%|            Go to matching (), {}, [], /* */, #if, #else, #endif.
  54. |[/|            Go to previous start of comment.
  55. |]/|            Go to next end of comment.
  56. |[#|            Go back to unclosed #if, #ifdef, or #else.
  57. |]#|            Go forward to unclosed #else or #endif.
  58. |[(|            Go back to unclosed '('
  59. |])|            Go forward to unclosed ')'
  60. |[{|            Go back to unclosed '{'
  61. |]}|            Go forward to unclosed '}'
  62.  
  63. |v_ab|            Select "a block" from "[(" to "])", including braces
  64. |v_ib|            Select "inner block" from "[(" to "])"
  65. |v_aB|            Select "a block" from "[{" to "]}", including brackets
  66. |v_iB|            Select "inner block" from "[{" to "]}"
  67.  
  68. ==============================================================================
  69. Finding where identifiers are used            *ident-search*
  70.  
  71. Sometimes you wish you could jump to where a function or variable is being
  72. used.  This is possible in two ways:
  73. 1. Using "grep" and quickfix commands.  This should work on most Unix systems,
  74.    but can be slow and only searches in one directory
  75. 2. Using ID utils.  This is fast and works in multiple directories.  You will
  76.    need some additional programs for this to work.
  77.  
  78. 1. Using grep.
  79.  
  80. Add one long line to your .vimrc:
  81. >  map _g :let efsave=&ef<Bar>let &ef=tempname()<Bar>exe ':!grep -n -w "<cword>" *.[ch] *.txt >'.&ef<CR>:cf<CR>:exe ":!rm ".&ef<CR>:let &ef=efsave<Bar>unlet efsave<CR><CR>:cc<CR>
  82.  
  83. That's all!  You can use this by placing the cursor on any identifier, and
  84. hitting "_g".  Then go to further matches with ":cn".  ":cc" and ":cp" can be
  85. used to move around in the list of matches.  Use ":cl" to see all matches.
  86.  
  87. This depends on the format of what grep produces and 'errorformat'.  It should
  88. work with the default settings.
  89.  
  90. Not all versions of "grep" accept the "-w" argument.  You can try if this
  91. works instead:
  92. >    :map _g :!grep -n "\<<cword>\>" *.[ch] >errors.vim<CR>:cf errors.vim<CR>
  93.  
  94.  
  95. 2. With a combination of Vim and the GNU id-tools.
  96.  
  97. This is still is a rather simple tool, but it works.
  98.  
  99. What you need:
  100. - Vim 5.0 or later.
  101. - The GNU id-tools installed (mkid is needed to create ID and lid is needed to
  102.   use the macros).
  103. - An identifier database file called "ID" in the current directory.
  104.  
  105. Put this in your .vimrc:
  106. >    :map su :source $HOME/.vim/such.vim<CR>
  107. >    :map nn :n<CR>n
  108.  
  109. Put this in the "$HOME/.vim/such.vim" file:
  110. >    " such.vim
  111. >    " lets you find arbitrary strings from the GNU mkid utility
  112. >    " kind of a reverse tags system
  113. >    :let o = expand("<cword>")
  114. >    :execute "let b = \"`lid --key=none " o "`\""
  115. >    :let x = expand( b )
  116. >    :execute "n " . x
  117. >    :execute "/\\<".o."\\>"
  118.  
  119. To use it, just step on a word with the cursor, type su and vim should load
  120. the file[s] that contain the word.  Search for the next occurence with "n" and
  121. go to the next file with "nn".
  122.  
  123. This has been tested with id-utils-3.2 (which is the name name of the id-tools
  124. archive file on your closest gnu-ftp-mirror).
  125.  
  126. [the idea for this comes from Andreas Kutschera]
  127.  
  128. ==============================================================================
  129. Editing local HTML files (WWW)                *html-editing*
  130.  
  131. Vim has some features which can help simplify the creation and maintenance of
  132. HTML files, provided that the files you are editing are available on the local
  133. file system.  The "]f", "gf" and "CTRL-W f" commands can be used to jump to
  134. the file whose name appears under the cursor, thus not only checking that the
  135. link is valid (at least the file name part of the URL) but also providing a
  136. quick and easy way to edit many related HTML pages at once.  |gf|
  137. A set of macros to help with editing html can be found on the Vim pages. |www|
  138.  
  139. If you want to view your HTML file, after writing a new version with Vim, have
  140. a look at the "atchange" program.  It can be used to automatically start a
  141. viewer when the last-changed date/time of a file changes.  You can find info
  142. at: <URL:http://www-lmmb.ncifcrf.gov/~toms/atchange.html>
  143.  
  144. ==============================================================================
  145. Editing paragraphs without a line break            *edit-no-break*
  146.  
  147. If you are typing text in Vim that will later go to a word processor, it is
  148. useful to keep a paragraph as a single line.  To make this more easy:
  149. - set 'wrap' on, to make lines wrap                |'wrap'|
  150. - set 'linebreak' on, to make wrapping happen at a blank    |'linebreak'|
  151. - use "gk" and "gj" to move one screen line up or down        |gj|
  152.  
  153. ==============================================================================
  154. Switching screens in an xterm        *xterm-screens* *xterm-save-screen*
  155.  
  156. (From comp.editors, by Juergen Weigert, in reply to a question)
  157.  
  158. :> Another question is that after exiting vim, the screen is left as it
  159. :> was, i.e. the contents of the file I was viewing (editting) was left on
  160. :> the screen. The output from my previous like "ls" were lost,
  161. :> ie. no longer in the scrolling buffer. I know that there is a way to
  162. :> restore the screen after exiting vim or other vi like editors,
  163. :> I just don't know how. Helps are appreciated. Thanks.
  164. :
  165. :I imagine someone else can answer this.  I assume though that vim and vi do
  166. :the same thing as each other for a given xterm setup.
  167.  
  168. They not necessarily do the same thing, as this may be a termcap vs.
  169. terminfo problem. You should be aware that there are two databases for
  170. describing attributes of a particular type of terminal: termcap and
  171. terminfo. This can cause differences when the entries differ AND when of
  172. the programs in question one uses terminfo and the other uses termcap
  173. (also see |+terminfo|).
  174.  
  175. In your particular problem, you are looking for the control sequences
  176. ^[[?47h and ^[[?47l. These switch between xterms alternate and main screen
  177. buffer. As a quick workaround a command sequence like
  178. >    echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l"
  179. may do what you want. (My notation ^[ means the ESC character, further down
  180. you'll see that the databases use \E instead).
  181.  
  182. On startup, vim echoes the value of the termcap variable ti (terminfo:
  183. smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup). Thus
  184. these two variables are the correct place where the above mentioned control
  185. sequences should go.
  186.  
  187. Compare your xterm termcap entry (found in /etc/termcap) with your xterm
  188. terminfo entry (retrieved with /usr/5bin/infocmp -C xterm). Both should
  189. contain entries similar to:
  190. >    :te=\E[2J\E[?47l\E8:ti=\E7\E[?47h:
  191.  
  192. PS: If you find any difference, someone (your sysadmin?) should better check
  193.     the complete termcap and terminfo database for consistency.
  194.  
  195. NOTE: If you recompile Vim with SAVE_XTERM_SCREEN defined in feature.h, the
  196. builtin xterm will include the mentioned "te" and "ti" entries.
  197.  
  198. NOTE 2: If you want to disable the screen switching, and you don't want to
  199. change your termcap, you can add these lines to your .vimrc:
  200. >    :set t_ti= t_te=
  201.  
  202. ==============================================================================
  203. Scrolling in Insert mode                *scroll-insert*
  204.  
  205. If you are in insert mode and you want to see something that is just off the
  206. screen, you can use CTRL-X CTRL-E and CTRL-X CTRL-Y to scroll the screen.
  207.                         |i_CTRL-X_CTRL-E|
  208.  
  209. To make this easier, you could use these mappings:
  210. >    :inoremap ^E ^X^E
  211. >    :inoremap ^Y ^X^Y
  212. (Type CTRL-V CTRL-E to enter ^E, CTRL-V CTRL-X to enter ^X, etc.)
  213.  
  214. Also consider setting 'scrolloff' to a larger value, so that you can always see
  215. some context around the cursor.  If 'scrolloff' is bigger than half the window
  216. height, the cursor will always be in the middle and the text is scrolled when
  217. the cursor is moved up/down.
  218.  
  219. ==============================================================================
  220. Smooth scrolling                    *scroll-smooth*
  221.  
  222. If you like the scrolling to go a bit smoother, you can use these mappings:
  223. >    :map ^U ^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y^Y
  224. >    :map ^D ^E^E^E^E^E^E^E^E^E^E^E^E^E^E^E^E
  225.  
  226. (Type CTRL-V CTRL-U to enter ^U, CTRL-V CTRL-Y to enter ^Y, etc.)
  227.  
  228. ==============================================================================
  229. Correcting common typing mistakes            *type-mistakes*
  230.  
  231. When there are a few words that you keep on typing in the wrong way, make
  232. abbreviations that correct them.  For example:
  233. >    :ab teh the
  234. >    :ab fro for
  235.  
  236. ==============================================================================
  237. Renaming files                        *rename-files*
  238.  
  239. Say I have a directory with the following files in them (directory picked at
  240. random :-):
  241.  
  242. addcr.c
  243. alloc.c
  244. amiga.c
  245. ...
  246.  
  247. and I want to rename *.c *.bla.  I'd do it like this:
  248.  
  249. >    $ vim
  250. >    :r! ls *.c
  251. >    :%s/\(.*\).c/mv & \1.bla
  252. >    :w !sh
  253. >    :q!
  254.  
  255. ==============================================================================
  256. Speeding up external commands                *speed-up*
  257.  
  258. In some situations, execution of an external command can be very slow.  This
  259. can also slow down wildcard expansion on Unix.  Here are a few suggestions to
  260. increase the speed.
  261.  
  262. If your .cshrc (or other file, depending on the shell used) is very long, you
  263. should separate it into a section for interactive use and a section for
  264. non-interactive use (often called secondary shells).  When you execute a
  265. command from Vim like ":!ls", you do not need the interactive things (for
  266. example, setting the prompt).  Put the stuff that is not needed after these
  267. lines:
  268.  
  269. >    if ($?prompt == 0) then
  270. >        exit 0
  271. >    endif
  272.  
  273. Another way is to include the "-f" flag in the 'shell' option, e.g.:
  274.  
  275. >    :set shell=csh\ -f
  276.  
  277. (the backslash is needed to include the space in the option).
  278. This will make csh completely skip the use of the .cshrc file.  This may cause
  279. some things to stop working though.
  280.  
  281. ==============================================================================
  282. Useful mappings                        *useful-mappings*
  283.  
  284. Here are a few mappings that some people like to use.
  285.  
  286. >    :map ' `
  287. Make the single quote work like a backtick.  Puts the cursor on the column of
  288. a mark, instead of going to the first non-blank character in the line.
  289.  
  290.                             *emacs-keys*
  291. For Emacs-style editing on the command-line:
  292. >    " start of line
  293. >    :cnoremap ^A        <Home>
  294. >    " back one character
  295. >    :cnoremap ^B        <Left>
  296. >    " delete character under cursor
  297. >    :cnoremap ^D        <Del>
  298. >    " end of line
  299. >    :cnoremap ^E        <End>
  300. >    " forward one character
  301. >    :cnoremap ^F        <Right>
  302. >    " recall newer command-line
  303. >    :cnoremap ^N        <Down>
  304. >    " recall previous (older) command-line
  305. >    :cnoremap ^P        <Up>
  306. >    " back one word
  307. >    :cnoremap <Esc>^B    <S-Left>
  308. >    " forward one word
  309. >    :cnoremap <Esc>^F    <S-Right>
  310.  
  311.                             *format-bullet-list*
  312. This mapping will format any bullet list.  It requires that there is an empty
  313. line above and below each list entry.  The expression commands are used to
  314. be able to give comments to the parts of the mapping.
  315.  
  316. >    let m =     ":map _f  :set ai<CR>"    " need 'autoindent' set
  317. >    let m = m . "{O<Esc>"              " add empty line above item
  318. >    let m = m . "}{)^W"              " move to text after bullet
  319. >    let m = m . "i     <CR>     <Esc>"    " add space for indent
  320. >    let m = m . "gq}"              " format text after the bullet
  321. >    let m = m . "{dd"              " remove the empty line
  322. >    let m = m . "5lDJ"              " put text after bullet
  323. >    exec m                      " define the mapping
  324.  
  325. (<> notation |<>|.  Note that ^W is "^" "W", not CTRL-W.  You can copy/paste
  326. this into Vim if '<' is not included in 'cpoptions')
  327.  
  328. You also need to set 'textwidth' to a non-zero value, e.g.,
  329. >    set tw=70
  330.  
  331. A mapping that does about the same, but takes the indent for the list from the
  332. first line (Note: this mapping is a single long line with a lot of spaces):
  333. >    :map _f :set ai<CR>}{a                                                          <Esc>WWmmkD`mi<CR><Esc>kkddpJgq}'mJO<Esc>j
  334.  
  335. To count the number of words in the current buffer:
  336. >    :map _wc :%s/[^ <Tab>]\+/&/g<CR>``
  337. The number of changes reported is the number of words.  You can do "u" to undo
  338. the substitute (although the text isn't really modified).
  339.  
  340. ==============================================================================
  341. Compressing the help files                *gzip-helpfile*
  342.  
  343. For those of you who are really short on disk space, you can compress the help
  344. files and still be able to view them with Vim.  This makes accessing the help
  345. files a bit slower.
  346.  
  347. (1) Compress all the help files: "gzip doc/*.txt".
  348.  
  349. (2) Edit "doc/tags" and do
  350. >    :%s=\.txt\t/=.txt.gz\t/=
  351.  
  352. (3) Add these lines to your vimrc:
  353. >    set helpfile=<dirname>/help.txt.gz
  354. >    autocmd BufReadPre  *.txt.gz set bin
  355. >    autocmd BufReadPost *.txt.gz '[,']!gunzip
  356. >    autocmd BufReadPost *.txt.gz set nobin
  357. >    set cmdheight=2
  358.  
  359. Where <dirname> is the directory where the help files are.  If you have
  360. already included autocommands to edit ".gz" files, you should omit the three
  361. "autocmd" lines.  Setting 'cmdheight' to two is to avoid the |hit-return|
  362. prompt; it is not mandatory.  You must also make sure that $VIM is set to
  363. where the other Vim files are, when they are not in the same location as the
  364. compressed "doc" directory.  See |$VIM|.
  365.  
  366. ==============================================================================
  367. Executing shell commands in a window            *shell-window*
  368.  
  369. There have been questions for the possibility to execute a shell in a window
  370. inside Vim.  The answer: you can't!  Including this would add a lot of code to
  371. Vim, which is a good reason not to do this.  After all, Vim is an editor, it
  372. is not supposed to do non-editing tasks.  However, to get something like this,
  373. you might try splitting your terminal screen or display window with the
  374. "splitvt" program.  You can probably find it on some ftp server.  The person
  375. that knows more about this is Sam Lantinga (<slouken@cs.ucdavis.edu>).
  376.  
  377. ==============================================================================
  378. Using <> notation in autocommands            *autocmd-<>*
  379.  
  380. The <> notation is not recognized in the argument for :autocmd.  To avoid
  381. having to use special characters, you could use a self-destroying mapping to
  382. get the <> notation and then call the mapping from the autocmd.  Example:
  383.  
  384.                     *buffer-menu* *map-self-destroy*
  385. > " This is for automatically adding the name of the file to the menu list.
  386. > " It uses a self-destroying mapping!
  387. > " 1. use a line in the buffer to convert the 'dots' in the file name to \.
  388. > " 2. store that in register '"'
  389. > " 3. add that name to the Buffers menu list
  390. > " WARNING: this does have some side effects, like overwriting the
  391. > " current register contents and removing any mapping for the "i" command.
  392. > "
  393. > autocmd BufNewFile,BufReadPre * nmap i :nunmap i<CR>O<C-R>%<Esc>:.g/\./s/\./\\./g<CR>0"9y$u:menu Buffers.<C-R>9 :buffer <C-R>%<C-V><CR><CR>
  394. > autocmd BufNewFile,BufReadPre * normal i
  395.  
  396. ==============================================================================
  397. Using the "User" event for a function            *User-function*
  398.  
  399. Vim doesn't offer the possibility to define your own functions yet.  But you
  400. can mostly get the functionality with the |User| autocommand event.
  401.  
  402. The idea is to create an autocommand group, in which you put several commands.
  403. These commands can then be executed with the ":doautocmd" command.
  404.  
  405. This example shows how to push the current file onto a stack with the "gf"
  406. command, and be able to jump back with the ",p" mapping.  You can put this
  407. literally in your .vimrc file:
  408.  
  409. >  "initialize the stack variable.  It will contain "file3\nfile2\nfile1"
  410. >  let stackf = ""
  411. >
  412. >  aug pushfile
  413. >    au! User x let stackf = expand("%:p") . "\n" . stackf
  414. >  aug END
  415. >
  416. >  aug popfile
  417. >    au! User x let file = substitute(stackf, "\n.*", "", "")
  418. >    au  User x if file != ""
  419. >    au  User x   execute "e " . file
  420. >    au  User x   let stackf = substitute(stackf, "[^\n]*\n", "", "")
  421. >    au  User x endif
  422. >  aug END
  423. >
  424. >  noremap gf :do pushfile User x<C-M>gf
  425. >  noremap ,p :do popfile User x<C-M>
  426.  
  427. Notes:
  428. - The autocommands are not echoed, which works much better than putting the
  429.   commands in the mapping directly.
  430. - "au! is used as the first command in each group, to remove any previous
  431.   autocommands in this group.  This is to avoid trouble when sourcing the
  432.   .vimrc file twice.
  433.  
  434.  vim:ts=8:sw=8:tw=78:
  435.